package com.hengda.museumonline.base;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.hengda.museumonline.R;

import com.hengda.museumonline.fragment.EngineeringFragment;
import com.hengda.museumonline.fragment.TechnologyFragment;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.utils.TabIconsUtil;

import java.util.ArrayList;
import java.util.List;


public class PcrPager {
    private MainActivity mainActivity;
    private ViewPager viewpager;
    private TabLayout tabLayout;
    private String[] titles = {"工程", "技术"};
    private int[] tabIcons = {
            R.drawable.pcr_engineering2,
            R.drawable.pcr_technology2};
    private int[] tabIconsPressed = {
            R.drawable.pcr_engineering1,
            R.drawable.pcr_technology1};
    public View view;

    public PcrPager(MainActivity activity) {
        mainActivity = activity;
        initView();
        initData();
    }

    private void initView() {
        view = View.inflate(mainActivity, R.layout.pcrpager, null);
        viewpager = (ViewPager) view.findViewById(R.id.viewpager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabs);

    }


    public void initData() {
        if (viewpager != null) {
            setupViewPager(viewpager);
        }
        tabLayout.setupWithViewPager(viewpager);
        tabLayout.setSelectedTabIndicatorColor(mainActivity.getResources().getColor(R.color.refresh_bg));
        TabIconsUtil util = new TabIconsUtil(mainActivity, tabLayout, titles, tabIcons, tabIconsPressed);
        util.setTabIcons2();
    }

    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(mainActivity.getSupportFragmentManager());
        adapter.addFragment(new EngineeringFragment(mainActivity), "工程");
        adapter.addFragment(new TechnologyFragment(mainActivity), "技术");
        viewPager.setAdapter(adapter);
    }

    static class Adapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }
}
